//
//  COQATableViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/21.
//

import UIKit
import QMUIKit

class COQATableViewCell: UITableViewCell {
    var model: COHomeFoodListModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            avatar.kf.setImage(with: URL(string: model.sculpture))
            nameLabel.text = model.nickName
            let formatter = DateFormatter()
            formatter.dateFormat = "MMMM d, YYYY"
            timeLabel.text = formatter.string(from: model.createDate)
            contentLabel.text = model.content
            contentImageView.kf.setImage(with: URL(string: model.photo))
            if model.photo == "" {
                contentImageView.isHidden = true
            } else {
                contentImageView.isHidden = false
            }
            
            tagLabel.setTitle(model.type.tag, for: .normal)
            likeButton.setTitle("\(model.likes)", for: .normal)
            likeButton.isSelected = model.like
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        backgroundColor = .rgba(17, 7, 41, 1)
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(stackView)
        stackView.addArrangedSubview(contentLabel)
        stackView.addArrangedSubview(contentImageView)
        contentView.addSubview(tagLabel)
        contentView.addSubview(likeButton)
        contentView.addSubview(lineView)
        
        avatar.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(20)
            make.width.height.equalTo(30)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.top.equalTo(avatar.snp.top).offset(0)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.bottom.equalTo(avatar.snp.bottom).offset(0)
        }
        
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(avatar.snp.bottom).offset(15)
        }
        
        contentImageView.snp.makeConstraints { make in
            make.height.equalTo(200)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalTo(stackView.snp.bottom).offset(10)
        }
        
        likeButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-18)
            make.centerY.equalTo(tagLabel)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview().offset(0)
            make.top.equalTo(stackView.snp.bottom).offset(50)
            make.height.equalTo(10)
        }
    }

    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 15
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(15)
        label.textColor = .white
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(12)
        label.textColor = .white.withAlphaComponent(0.5)
        return label
    }()
    
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .vertical
        stackV.spacing = 17
        return stackV
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(15)
        label.textColor = .white
        label.numberOfLines = 0
        label.text = "dsalkjdljsadsljdsajklasjldajlkasljadsjlsajklaljskalkaljkasljkajlsaljkadjkslajkaljdlajdskladjkljadksjdaskajlkajljlasjklsljkaljkaljkasljkaskjlas"
        return label
    }()
    
    private lazy var contentImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 15
        imgV.contentMode = .scaleAspectFit
        return imgV
    }()
    
    private lazy var tagLabel: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("Italy", for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.contentEdgeInsets = .init(top: 3, left: 7, bottom: 3, right: 7)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        btn.layer.borderColor = UIColor.white.cgColor
        btn.layer.borderWidth = 1
        return btn
    }()
    
    private lazy var likeButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("122313", for: .normal)
        btn.imageForNormal = UIImage(named: "编组 11")
        btn.imageForSelected = UIImage(named: "编组 3")
        btn.imagePosition = .left
        btn.spacingBetweenImageAndTitle = 4
        return btn
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
}
